/*
 * File: GetMessages.java Author: Robert Bittle <guywithnose@gmail.com>
 */
package servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject;
import faceBook.User;

/**
 * The Class GetMessages.
 */
public class GetMessageCount extends AuthenticationServlet
{

  /*
   * (non-Javadoc)
   * 
   * @see
   * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
   * javax.servlet.http.HttpServletResponse)
   */
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException
  {
    if (req.getParameter("userid") == null
        || req.getParameter("accessToken") == null
        || !authenticate(Long.valueOf(req.getParameter("userid")),
            req.getParameter("accessToken")))
    {
      resp.getWriter().write(errorJson("Authentication failed."));
      return;
    }
    User user = new User(Long.valueOf(req.getParameter("userid")));

    JSONObject JO = new JSONObject();
    try
    {
      JO.put("count", user.getCustomMessageCount());
    } catch (JSONException e)
    {
      e.printStackTrace();
    }
    resp.getWriter().write(JO.toString());
  }

}
